//
//  BC_GameTreeCategories.m
//  GameTree
//
//  Created by BrunoCausse on 14/11/10.
//  Copyright 2010-2013 poulailler cie. All rights reserved.
//

#import "BC_GameTreeCategories.h"
#import "BC_Utils.h"


@implementation NSBrowser (additions)

/*
 reloadColumn:(NSUInteger)column : recharge la colonne column (doit etre deja presente) avec ses fils si ils existent 
 
 [self setSelectionIndexPath:indexPath]; //is not supported for browsers with matrix delegates (passif).
 */

- (void)updateUIAtIndexPath:(NSIndexPath *)indexPath
{

	//charge la colonne 0 si necessaire
	if (![self isLoaded]) {
		[self loadColumnZero]; //vide le browser et charge la 1er colonne
	}

	NSUInteger indexPathLength = [indexPath length];
	
	if(indexPathLength == 0) { //root
		[self loadColumnZero]; //vide le browser et charge la 1er colonne sans selection
	} else {
		
		NSUInteger iColumn = indexPathLength - 1 ;
		
		if(iColumn == 0) {
			
			[self loadColumnZero];
			[self selectRow:[indexPath lastIndex] inColumn:0];
			
		} else {
			
			NSUInteger lastColumn = [self lastColumn];
			
			if (iColumn <= lastColumn) {
				//la colonne existe

				[self reloadColumn:iColumn];

			} else {
				//la colonne n'existe pas mais son pere existe
				
				NSUInteger iParentColumn = iColumn -1;
				NSIndexPath *parentIndexPath = [indexPath indexPathByRemovingLastIndex];
				
				if (iParentColumn == 0) {
					//cas particulier colonne 0
					[self loadColumnZero]; //vide le browser et charge la 1er colonne
					[self selectRow:[parentIndexPath lastIndex] inColumn:0];

					
				} else {

					[self reloadColumn:iColumn-1];
					[self selectRow:[parentIndexPath lastIndex] inColumn:iColumn-1];

				}
				
			}
			
			[self selectRow:[indexPath lastIndex] inColumn:iColumn];
			
		}
	}
}



@end

@implementation NSArray (indexSetAdditions)

/*
 change le nom pour la convention
 */

- (NSIndexSet *)indexSetOfObjects:(NSArray *)array
{
	NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
	
	for(id object in array) {
		NSUInteger index = [self indexOfObject:object];
		if (index != NSNotFound) {
			[indexSet addIndex:index];
		}
	}
	
	/* 
	 [indexSet copy] realise une copie immutable? sous la forme d'un NSIndexSet
	 ATTENTION (copy) retainCount = 1, l'objet appelant est en charge de cet objet
	 */
	return [[indexSet copy] autorelease];
}

@end

@implementation NSIndexPath (browserAdditions)

- (NSUInteger)lastIndex
{
	if ([self length] != 0) {
		return[self indexAtPosition:[self length]-1];
	}
	
	return NSUIntegerMax;
}

- (NSIndexPath *)subIndexPath:(NSUInteger)length
{
	NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[self indexAtPosition:0]];
	for(NSUInteger i = 1; i < length; i++)
		indexPath = [indexPath indexPathByAddingIndex:[self indexAtPosition:i]];
	
	return indexPath;
}

/*
 change le nom pour la convention
 */


+ (NSIndexSet *)lastIndexes:(NSArray *)indexPaths
{
	NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
	for(id indexPath in indexPaths) {
		if (![indexPath isKindOfClass:[NSIndexPath class]]) {
			continue;
		}
		
		NSUInteger index = [indexPath lastIndex];
		if (index != NSNotFound) {
			[indexSet addIndex:index];
		}
	}

	/* 
	 [indexSet copy] realise une copie immutable sous la forme d'un NSIndexSet
	 ATTENTION (copy) retainCount = 1, l'objet appelant est en charge de cet objet
	 il faut autorelease
	 */
	
	return [[indexSet copy] autorelease];
		
}

/*
 retourne un tableau d'indexPath, 
 chaque elements est indexPath + index des cellules selectionnées
 */

- (NSArray *)indexPathsByAddingIndexes:(NSIndexSet *) indexSet
{
	NSUInteger count=[indexSet count];
	NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
	NSUInteger  indexes[count];
	[indexSet getIndexes:indexes maxCount:count inIndexRange:nil];
	
	for(NSInteger i = 0; i<count; i++) {
		[array addObject:[self indexPathByAddingIndex:indexes[i]]];
	}
	return [[array copy] autorelease];	//retourne un tableau non mutable
}


/* 
 debug
 */
- (NSString *)description
{
	NSUInteger length = [self length];
	NSMutableString * stringPath = [NSMutableString stringWithCapacity:(length*2)];
	for(int i = 0; i<length; i++) {
		[stringPath appendFormat:@"%ldd.",[self indexAtPosition:i]];
	}
	
	return stringPath;
}


@end



